//
//  MineTableViewController.m
//  GiftItems
//
//  Created by lanou3g on 15/9/18.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "MineTableViewController.h"
#import "MineTableViewCell.h"
#import "MineSecondTableViewCell.h"
#import "MIneHeaderView.h"
#import "BlurImageView.h"
#import "MineIDTableViewController.h"
#import "MineCollectionTableViewController.h"
#import "MineAdviceViewController.h"
#import "MIneShakeViewController.h"
#import "MineIntroductionViewController.h"
#import "SweepViewController.h"
@interface MineTableViewController ()<UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property(nonatomic, strong) MIneHeaderView *headView;
@property(nonatomic, strong) NSString *gender;
@property(nonatomic, strong) NSString *role;
@property(nonatomic, strong) UISwitch *Switch;
@end

@implementation MineTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.backgroundColor = [UIColor blackColor];
    
    self.tableView.backgroundView = [[BlurImageView alloc] initWithFrame:kBounds];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.tableView registerClass:[MineTableViewCell class] forCellReuseIdentifier:@"myCell"];
    [self.tableView registerClass:[MineSecondTableViewCell class] forCellReuseIdentifier:@"secCell"];
    self.tableView.sectionHeaderHeight = 200;
    [self setHeadViewForTableView];
    self.tableView.alwaysBounceVertical = NO;
    self.gender = @" ";
    self.role = @" ";
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sendGender:) name:@"gender" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sendRole:) name:@"role" object:nil];
    
}

// 视图消失时,移除观察者
- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"gender" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"role" object:nil];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
// 接受性别的通知
- (void)sendGender:(NSNotification *)no {
    // 移除上次保存的性别信息
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"gender"];
    [[NSUserDefaults standardUserDefaults] setObject:no.object forKey:@"gender"];;
}

// 接受角色的通知
- (void)sendRole:(NSNotification *)no {
    // 移除上次保存的角色信息
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"role"];
    [[NSUserDefaults standardUserDefaults] setObject:no.object forKey:@"role"];;
}
// 视图将要出现时,刷新表示图
- (void)viewWillAppear:(BOOL)animated {
    [self.tableView reloadData];
}

// 为表头添加手势
- (void)setHeadViewForTableView {
    self.headView = [[MIneHeaderView alloc] initWithFrame:CGRectMake(100, 0, 200, 100)];
    self.tableView.tableHeaderView = self.headView;
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectPicImage)];
    [self.headView addGestureRecognizer:tapGR];
}
// 手势的点击事件
- (void)selectPicImage {
    
    // 创建从底部弹出的提醒视图
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"提醒" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"拍照" otherButtonTitles:@"相册", nil];
    sheet.actionSheetStyle = UIActionSheetStyleDefault;
    [sheet showInView:self.view];
}

// 实现代理的方法
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    if (buttonIndex == 0) {
        
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            picker.delegate = self;
            picker.allowsEditing = YES;
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            [self presentViewController:picker animated:YES completion:nil];
        }else {
            // 如果没有系统相机,则提醒用户当前设备没有相机
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"你的设备没有摄像头" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alert show];
        
        }
    }
    if (buttonIndex == 1) {
        
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            picker.delegate = self;
            picker.allowsEditing = YES;
            picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            [self presentViewController:picker animated:YES completion:nil];
        }
    }
}

#pragma mark -- 拍摄完成或者从相册中选择完成之后执行的方法 ---

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    self.headView.picImage.image = [info objectForKey:UIImagePickerControllerEditedImage];
    [picker dismissViewControllerAnimated:YES completion:nil];
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
     // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        MineSecondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"secCell" forIndexPath:indexPath];
        cell.title.text = @"我的身份:";
        cell.picImage.image = [UIImage imageNamed:@"0.jpg"];
        NSString *gender = [[NSUserDefaults standardUserDefaults] objectForKey:@"gender"];
        NSString *role = [[NSUserDefaults standardUserDefaults] objectForKey:@"role"];
        if (gender == nil || role == nil) {
            cell.idLabel.text = @"  ";
            return cell;
        }
        NSString *idStr = [NSString stringWithFormat:@"%@  %@",gender,role];
        
        cell.idLabel.text = idStr;
        return cell;
        
    }else if(indexPath.row > 0 && indexPath.row <= 4) {
        
        NSArray *array = @[@"我的收藏",@" 意见反馈",@"摇一摇",@"扫一扫"];
        NSArray *images = @[@"1.jpg",@"2.jpg",@"3.jpg",@"4.jpg"];
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.text = array[indexPath.row - 1];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.imageView.image = [UIImage imageNamed:images[indexPath.row - 1]];
        return cell;
        
    }else if(indexPath.row > 4 && indexPath.row <= 6){
        NSArray *images = @[@"5.jpg",@"6.jpg"];
        NSArray *array = @[@"接受消息推送",@"夜间模式"];
        MineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell" forIndexPath:indexPath];
        cell.title.text = array[indexPath.row - 5];
        if (indexPath.row == 6) {
            [cell.Switch addTarget:self action:@selector(changeLight) forControlEvents:UIControlEventValueChanged];
        }
        cell.picImgae.image = [UIImage imageNamed:images[indexPath.row - 5]];
        return cell;
    }else {
        NSArray *images = @[@"7.jpg",@"8.jpg",@"9.jpg"];
        NSArray *array = @[@"清除缓存",@"检测版本",@"关于礼物"];
        MineSecondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"secCell" forIndexPath:indexPath];
        cell.title.text = array[indexPath.row - 7];
        if (indexPath.row == 7) {
            // 换取缓存的大小,转化成MB的倍数
            NSUInteger cacheSize = [SDImageCache sharedImageCache].getSize;
            NSString *cache = [NSString stringWithFormat:@"%.2fMB",cacheSize/1024.0/1024.0];
            cell.idLabel.text = cache;
        }
        cell.picImage.image = [UIImage imageNamed:images[indexPath.row - 7]];
        return cell;
    }
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.row) {
        case 0:
        {
            MineIDTableViewController *mineIDVC = [[MineIDTableViewController alloc] init];
            [self.navigationController pushViewController:mineIDVC animated:YES];
        
            break;
        }
        case 1:
        {
            MineCollectionTableViewController *mineVC = [[MineCollectionTableViewController alloc] init];
            [self.navigationController pushViewController:mineVC animated:YES];
            break;
        }
        case 2:
        {
            MineAdviceViewController *adviceVC = [[MineAdviceViewController alloc] init];
            [self.navigationController pushViewController:adviceVC animated:YES];
            break;
        }
        case 3:
        {
            MIneShakeViewController *shakeVC = [[MIneShakeViewController alloc] init];
            [self.navigationController pushViewController:shakeVC animated:YES];
            break;
        }
        case 4:
        {
            SweepViewController *sweepVC = [[SweepViewController alloc] init];
            [self.navigationController pushViewController:sweepVC animated:YES];
            NSLog(@"我的扫一扫");
            break;
        }
        case 5:
            NSLog(@"我的接受消息通知");
            break;
        case 6:
            NSLog(@"我的夜间模式");
            [self changeLight];
            break;
        case 7:
            NSLog(@"我的清除缓存");
            [self clearCache];
            break;
        case 8:
            [self addAlertView];
            break;
        case 9:
        {
            NSLog(@"我的关于礼物");
            MineIntroductionViewController *introductionVC = [[MineIntroductionViewController alloc] init];
            [self.navigationController pushViewController:introductionVC animated:YES];
            break;
        }
    }
}

- (void)changeLight {
    self.Switch = (UISwitch *)[self.tableView viewWithTag:1000];
    // Switch.on 默认是关闭的
    if (self.Switch.on == NO) {
        [[UIScreen mainScreen] setBrightness:0.5];
    }else {
        [[UIScreen mainScreen] setBrightness:0.05];
    }
}


// 弹出检测版本的信息
- (void)addAlertView {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示信息" message:@"当前为最新版本:1.0" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil,nil];
    [alertView show];
}

// 清除缓存的方法
- (void)clearCache {
    [[SDImageCache sharedImageCache] clearDisk];
    [[SDImageCache sharedImageCache] clearMemory];
    [self.tableView reloadData];
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
